package cn.vehicle.biz.impl;

import cn.vehicle.biz.SysPermissionBiz;
import cn.vehicle.domains.bo.QueryPermissionBo;
import cn.vehicle.domains.entity.SysPermissionEntity;
import cn.vehicle.domains.vo.BasePage;
import cn.vehicle.domains.vo.req.sysPermission.AddPermissionReq;
import cn.vehicle.domains.vo.req.sysPermission.PermissionListReq;
import cn.vehicle.domains.vo.req.sysPermission.UpdatePermissionReq;
import cn.vehicle.domains.vo.res.sysPermission.SysPermissionRes;
import cn.vehicle.exception.VehicleAccessException;
import cn.vehicle.service.SysPermissionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统权限业务实现类
 * <p>
 * 负责权限的新增、修改、删除、查询与分页列表等业务逻辑。
 * 主要负责参数校验、实体装配与分页封装，持久化逻辑委托给 {@link SysPermissionService}。
 * </p>
 *
 * <p>设计约定：</p>
 * <ul>
 *   <li>所有写操作（增删改）均通过 {@link SysPermissionService} 完成；</li>
 *   <li>操作前需校验数据存在性，异常统一抛出 {@link VehicleAccessException}；</li>
 *   <li>分页列表基于 PageHelper 实现。</li>
 * </ul>
 */
@Service
public class SysPermissionBizImpl implements SysPermissionBiz {

    @Resource
    private SysPermissionService sysPermissionService;

    /**
     * 新增权限
     *
     * @param req 新增权限请求参数
     *            方法内完成请求参数到实体对象的属性拷贝，并调用 Service 层执行新增操作。
     */
    @Override
    public void addPermission(AddPermissionReq req) {
        // 创建实体对象并拷贝请求参数
        SysPermissionEntity sysPermissionEntity = new SysPermissionEntity();
        BeanUtils.copyProperties(req, sysPermissionEntity);

        // 调用 Service 层进行新增
        sysPermissionService.addPermission(sysPermissionEntity);
    }

    /**
     * 更新权限
     *
     * <p>执行更新前会先校验权限是否存在。</p>
     *
     * @param req 更新权限请求参数，需包含主键ID
     * @throws VehicleAccessException 权限不存在时抛出
     */
    @Override
    public void updatePermission(UpdatePermissionReq req) {
        checkPermissionExist(req.getId());

        SysPermissionEntity updateEntity = new SysPermissionEntity();
        BeanUtils.copyProperties(req, updateEntity);
        sysPermissionService.updatePermission(updateEntity);
    }

    /**
     * 删除权限
     *
     * @param id 权限主键ID
     * @throws VehicleAccessException 权限不存在时抛出
     */
    @Override
    public void deletePermission(Long id) {
        checkPermissionExist(id);

        sysPermissionService.deletePermission(id);
    }

    /**
     * 根据ID查询权限详情
     *
     * @param id 权限主键ID
     * @return 权限详情响应对象
     * @throws VehicleAccessException 权限不存在时抛出
     */
    @Override
    public SysPermissionRes getPermissionById(Long id) {
        SysPermissionEntity sysPermissionEntity = checkPermissionExist(id);

        SysPermissionRes sysPermissionRes = new SysPermissionRes();
        BeanUtils.copyProperties(sysPermissionEntity, sysPermissionRes);
        return sysPermissionRes;
    }

    /**
     * 分页查询权限列表
     *
     * <p>基于 PageHelper 进行分页查询。</p>
     *
     * @param req 查询条件与分页参数
     * @return 分页结果封装
     */
    @Override
    public BasePage<SysPermissionRes> list(PermissionListReq req) {
        // 启动分页拦截，仅作用于本线程下一条查询
        Page<SysPermissionEntity> page = PageHelper.startPage(req.getPageNo(), req.getPageSize());

        // 构建查询业务对象（BO），避免直接使用请求对象
        QueryPermissionBo queryPermissionBo = new QueryPermissionBo();
        // 拷贝查询条件
        BeanUtils.copyProperties(req, queryPermissionBo);

        // 执行查询，获取权限实体列表
        List<SysPermissionEntity> selectPermissionList = sysPermissionService.selectPermissionList(queryPermissionBo);
        // 实体转响应对象
        List<SysPermissionRes> list = selectPermissionList.stream().map(sysPermissionEntity -> {
            SysPermissionRes sysPermissionRes = new SysPermissionRes();
            BeanUtils.copyProperties(sysPermissionEntity, sysPermissionRes);

            return sysPermissionRes;
        }).toList();
        // 封装分页结果返回
        return BasePage.generateResult(page.getPages(), page.getTotal(), list);
    }



    /**
     * 校验权限是否存在
     *
     * @param id 权限主键ID
     * @return 已存在的权限实体
     * @throws VehicleAccessException 权限不存在时抛出 DATE_NOT_EXIST
     */
    private SysPermissionEntity checkPermissionExist(Long id) {
        SysPermissionEntity sysPermissionEntity = sysPermissionService.getPermissionById(id);

        if (sysPermissionEntity == null) {
            throw VehicleAccessException.DATE_NOT_EXIST.newInstance("权限不存在");
        }

        return sysPermissionEntity;
    }
}
